import axios from 'axios'
import util from './util.js'
import Cookies from 'js-cookie'
import Vue from 'vue'
import iView from 'iview'
import router from '../router/index.js'
Vue.use(iView)
axios.defaults.timeout = 5000
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded'
axios.defaults.headers['Authorization'] = 'Bearer ' + Cookies.get('token')

axios.interceptors.request.use(config => {
  config.headers.Authorization = 'Bearer ' + Cookies.get('token')
  // config.headers['X-Requested-With'] = 'XMLHttpRequest'
  return config
}, (error) => {
  // 对请求错误做些什么
  return Promise.reject(error)
})

let baseUrl = util.ajaxUrl

// 拦截响应response，并做一些错误处理
axios.interceptors.response.use((response) => {
  const data = response.data
  if (data.code !== 0) {
    // 若不是正确的返回code，且已经登录，就抛出错误
    const err = new Error(data.msg)
    err.response = response
    err.message = data.msg

    if (data.type === 'not-login') {
      Cookies.remove('token')
      Cookies.remove('userInfo')
      router.push({
        name: 'login'
      })
      Vue.prototype.$Message.error(err.message)
    }
    return Promise.reject(err)
  }
  return Promise.resolve(data)
}, (err) => { // 这里是返回状态码不为200时候的错误处理
  if (err && err.response) {
    switch (err.response.status) {
      case 400:
        err.message = '请求错误'
        break

      case 401:
        err.message = '未授权，请登录'
        break

      case 403:
        err.message = '拒绝访问'
        break

      case 404:
        err.message = `请求地址出错: ${err.response.config.url}`
        break

      case 408:
        err.message = '请求超时'
        break

      case 500:
        err.message = '服务器内部错误'
        break

      case 501:
        err.message = '服务未实现'
        break

      case 502:
        err.message = '网关错误'
        break

      case 503:
        err.message = '服务不可用'
        break

      case 504:
        err.message = '网关超时'
        break

      case 505:
        err.message = 'HTTP版本不受支持'
        break

      default:
    }
  }

  Vue.prototype.$Message.error(err.message)

  return Promise.reject(err)
})

export default {
  fetchGet (url, params = {}) {
    return new Promise((resolve, reject) => {
      axios.get(baseUrl + url, {
        params: params
      }).then(res => {
        resolve(res)
      }).catch(error => {
        reject(error)
      })
    })
  },
  fetchPost (url, params = {}) {
    return new Promise((resolve, reject) => {
      axios.post(baseUrl + url, params).then(res => {
        resolve(res)
      }).catch(error => {
        reject(error)
      })
    })
  },
  fetchPut (url, params = {}) {
    return new Promise((resolve, reject) => {
      axios.put(baseUrl + url, params).then(res => {
        resolve(res)
      }).catch(error => {
        reject(error)
      })
    })
  },
  fetchDelete (url, params = {}) {
    return new Promise((resolve, reject) => {
      axios.delete(baseUrl + url, {params: params}).then(res => {
        resolve(res)
      }).catch(error => {
        reject(error)
      })
    })
  },
  fetchPatch (url, params = {}) {
    return new Promise((resolve, reject) => {
      axios.patch(baseUrl + url, params).then(res => {
        resolve(res)
      }).catch(error => {
        reject(error)
      })
    })
  }
}
